package com.cntaiping.ttnews.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.widget.VideoView;

import com.cntaiping.ttnews.R;
import com.cntaiping.ttnews.activity.VideoDetailActivity;
import com.cntaiping.ttnews.adapter.VideoAdapter;
import com.cntaiping.ttnews.app.DividerItemDecoration;
import com.cntaiping.ttnews.bean.VideoDataBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.util.PtrLocalDisplay;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2016/7/21.
 */
public class VideoFragment extends Fragment {
    private String VOIDEOURL = "http://r.cnews.qq.com/getSubNewsChlidInterest?last_id=&omgid=7f9ad0c71935944df6b929f45578ffd92cd60010211615&forward=0&mid=694aa03777e8a17949e7bb7607f3bd04b92a008d&devid=sim_00000000000000&qqnetwork=wifi&last_time=1469102732&bottom_id=&kankaninfo=%7B%22needCollect%22%3A0%2C%22refresh%22%3A0%2C%22gender%22%3A1%7D&apptype=android&hw=Genymotion_genymotion_vbox86p_6.0_160609_160125&top_time=&appversion=2.2.0&appver=23_areading_2.2.0&top_id=&screen_width=720&omgbizid=84fc1f7589d6c741ebd9eda30a2a23a707ec0080211615&sceneid=&page=6&imsi=310260000000000&android_id=e96e80e71634dca0&mac=mac%2Bunknown&store=5&cachedCount=20&screen_height=1208&direction=0&Cookie=%20lskey%3D%3B%20luin%3D%3B%20skey%3D%3B%20uin%3D%3B%20logintype%3D0%20&chlid=kb_video_news&bottom_time=&uid=e96e80e71634dca0&qn-sig=f002305be63bacf8b61adb49dd1c8336&qn-rid=2117373119";
   private String mp4 = "http://bvideo.spriteapp.cn/video/2016/0610/575add617fc8b_wpd.mp4";
    private PtrClassicFrameLayout ptr;
    private RecyclerView recyclerView;
    private List<VideoDataBean.NewslistBean> mList;
    private Activity mActivity;
    private VideoAdapter adapter;
    private VideoDataBean bean;
    private VideoView videoview;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mActivity = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_video ,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ptr = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_video);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_video);
        bean = new VideoDataBean();
        adapter = new VideoAdapter(mActivity , bean);
        loadVideoData();
       RecyclerView.LayoutManager manager = new LinearLayoutManager(mActivity);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(manager);
        //设置阻尼系数，越大，感到越难往下拉
        ptr.setResistance(1.5f);
        //是否是下拉刷新
        //true:下拉到一定位置刷新
        //false:释放刷新
        ptr.setPullToRefresh(false);
        //头部高度的几倍再去刷新
        ptr.setRatioOfHeaderHeightToRefresh(1.5f);
        //刷新的时候，是否保持头部
        ptr.setKeepHeaderWhenRefresh(true);
        //恢复到头部的位置的时长
        ptr.setDurationToClose(200);
        //头部整体关闭的时常
        ptr.setDurationToCloseHeader(1000);

        ptr.setPtrHandler(new PtrDefaultHandler() {
          /*  @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return true;
            }*/

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptr.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadVideoData();
                        ptr.refreshComplete();
                    }
                },3000);
            }
        });



    }

    public void initView(VideoDataBean videoBean){
        bean = videoBean;
        adapter = new VideoAdapter(mActivity , bean);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(mActivity);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new DividerItemDecoration(mActivity , LinearLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);
        mList = videoBean.getNewslist();
        adapter.dataChange(videoBean);
        adapter.notifyDataSetChanged();
        //点击监听
        adapter.setOnVideoClickListener(new VideoAdapter.OnVideoClickListener() {
            //点了ll 跳转到详情页
            @Override
            public void llClick(View view, int position) {
                Toast.makeText(mActivity, "ll:"+position, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(mActivity , VideoDetailActivity.class

                );
               intent.putExtra("mp4" , mp4);
                startActivity(intent);
            }
        });
    }

    public void loadVideoData(){
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(VOIDEOURL).build();
        Call call =okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                String videoJson = body.string();
                final VideoDataBean videoBean = new Gson().fromJson(videoJson, VideoDataBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        initView(videoBean);
                    }
                });

            }
        });
    }

    Handler handler = new Handler();

    //播放视屏
    public void playVideo(){

    }

}
